//
//  CommunityLinkTableViewCell.m
//  WeCommunity
//
//  Created by mac on 2017/8/9.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "CommunityLinkTableViewCell.h"
#import "CommunityLinkModel.h"

static CGFloat CellHeight = 96.0;

@interface CommunityLinkTableViewCell ()

@property (nonatomic,strong) UIView * lineView;             //分割线

@property (nonatomic,strong) UIImageView * contentImageView;

@property (nonatomic,strong) UILabel * contentLabel;

@property (nonatomic,strong) UILabel * timeLabel;

@property (nonatomic,strong) UIButton * likeButton;                 //转发按钮

@property (nonatomic,strong) UILabel * likeLabel;                   //评论数

@property (nonatomic,strong) CommunityLinkModel * model;

@end

@implementation CommunityLinkTableViewCell

#pragma mark - get
-(UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CellHeight - 0.5, KScreen_Width, 0.5)];
        _lineView.backgroundColor = LINE_COLOR;
    }
    return _lineView;
}

-(UIImageView *)contentImageView
{
    if (!_contentImageView) {
        _contentImageView = [[UIImageView alloc] init];
        _contentImageView.frame = CGRectMake(KScreen_Width - 114,12 , 100, 74);
        _contentImageView.contentMode = UIViewContentModeScaleAspectFill;
        _contentImageView.clipsToBounds = YES;
    }
    return _contentImageView;
}

-(UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textColor = [UIColor blackColor];
        _contentLabel.font = PFR_FONT(16);
    }
    return _contentLabel;
}

-(UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.textColor = [UIColor colorWithHex:0x959595 alpha:1.0];
        _timeLabel.font = PFR_FONT(12);
    }
    return _timeLabel;
}

-(UILabel *)likeLabel
{
    if (!_likeLabel) {
        _likeLabel = [[UILabel alloc] init];
        _likeLabel.textColor = [UIColor colorWithHex:0xa4a4a4 alpha:1.0];
        _likeLabel.font = PFR_FONT(14);
    }
    return _likeLabel;
}

-(UIButton *)likeButton
{
    if (!_likeButton) {
        _likeButton= [[UIButton alloc] init];
        _likeButton.exclusiveTouch = YES; //不允许同时多个按钮点击
        [_likeButton setImage:[UIImage imageNamed:@"com_unLike"] forState:UIControlStateNormal];
        [_likeButton setImage:[UIImage imageNamed:@"com_like"] forState:UIControlStateSelected];
        [_likeButton addTarget:self action:@selector(clickBottomBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _likeButton;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = WHITE_COLOR;
    
        [self.contentView addSubview:self.lineView];
        
        [self.contentView addSubview:self.contentLabel];
        
        [self.contentView addSubview:self.contentImageView];
        
        [self.contentView addSubview:self.timeLabel];
        
        [self.contentView addSubview:self.likeLabel];
        
        [self.contentView addSubview:self.likeButton];
        
    }
    return self;
}

-(void)creatCellWithModel:(CommunityLinkModel *)model
{
    self.model = model;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.contentImageView sd_setImageWithURL:LOADINGTHUMBIMG_URL(self.model.imageUrl,400,400)
                          placeholderImage:[UIImage imageNamed:@"placeholder"]
                                   options:SDWebImageRetryFailed
                                 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                     [[SDImageCache sharedImageCache] clearDisk];
                                 }];
        
        self.contentLabel.frame = CGRectMake(14, 14, KScreen_Width - 148, 44);
        self.contentLabel.text = self.model.content;
        self.contentLabel.numberOfLines = 2;
        [self.contentLabel sizeToFit];
        
        self.timeLabel.frame = CGRectMake(14, CellHeight - 28, 200, 16);
        self.timeLabel.text = self.model.formatTime;
        self.timeLabel.numberOfLines = 1;
        [self.timeLabel sizeToFit];
        
        //赞内容
        NSDictionary * attriDic = @{NSFontAttributeName:self.likeLabel.font};
        CGSize likeSize = [self.model.formatLikeTotalNum boundingRectWithSize:CGSizeMake(100, MAXFLOAT)
                                                                         options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                                      attributes:attriDic
                                                                         context:nil].size;
        CGFloat  likeWidth = likeSize.width + 10; //加个10 留点空间，防止数字改变，文字无法显示完全
        
        self.likeLabel.frame = CGRectMake(CGRectGetMinX(self.contentImageView.frame) - likeWidth - 14, CellHeight - 28, likeWidth, 16);
        self.likeLabel.text = self.model.formatLikeTotalNum;
        
        CGFloat imageWith = 25 + 4; //图片宽度25 + 4图片和文字间距
        //赞按钮
        self.likeButton.frame = CGRectMake(CGRectGetMinX(self.likeLabel.frame) - imageWith, CellHeight - 31, imageWith + likeWidth, 22);
        CGFloat edgeWidth = (likeWidth + 4 ) / 2; //4 图片和文字间距
        self.likeButton.imageEdgeInsets = UIEdgeInsetsMake(0, -edgeWidth, 0, edgeWidth);
        self.likeButton.layer.anchorPoint = CGPointMake(10.0 / (likeWidth + imageWith), 0.5);
        self.likeButton.frame = CGRectMake(CGRectGetMinX(self.likeLabel.frame) - imageWith, CellHeight - 31, imageWith + likeWidth, 22);
        
        self.likeButton.selected = self.model.isLike;
        if (self.model.isLike) {
            self.likeLabel.textColor = [UIColor colorWithHex:0xff6c6c alpha:1.0];
        }else{
            self.likeLabel.textColor = [UIColor colorWithHex:0xa4a4a4 alpha:1.0];
        }
        
        
        
    });
}

#pragma mark - 点赞
-(void)clickBottomBtn:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(communityLinkCellClickLikeSender:clickCell:)]) {
        [self.delegate communityLinkCellClickLikeSender:sender clickCell:self];
    }
}

#pragma mark - 放大缩小动画
-(void)likeAnimationWithSender:(UIButton*)sender
{
    if (sender.selected) {
        [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            sender.transform = CGAffineTransformMakeScale(1.5, 1.5);
        } completion:^(BOOL finished) {
            
            [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                sender.transform = CGAffineTransformMakeScale(1, 1);
            } completion:^(BOOL finished) {
                
            }];
        }];
    }
}

#pragma mark - 点赞数改变
-(void)changeLikeNumWithDynamic:(CommunityLinkModel*)model
{
    self.likeButton.selected = model.isLike;
    
    [self likeAnimationWithSender:self.likeButton];
    //改变数量
    self.likeLabel.text = model.formatLikeTotalNum;
    if (model.isLike) {
        self.likeLabel.textColor = [UIColor colorWithHex:0xff6c6c alpha:1.0];
    }else{
        self.likeLabel.textColor = [UIColor colorWithHex:0xa4a4a4 alpha:1.0];
    }
}

@end
